﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Gtkj.CMS.Common
{
    public class LogHelper
    {
        /**/
        /// <summary>
        /// 写入日志文件
        /// </summary>
        /// <param name="input"></param>
        public static void WriteLogFile(string input)
        {
            DateTime dt = DateTime.Now;
            /**/
            ///指定日志文件的目录
            string fname = "/log";
            /**/
            ///定义文件信息对象
            
            if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(fname)))
            {
                Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(fname));
            }
            fname += "/" + dt.ToString("yyyy-MM-dd");
            if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(fname)))
            {
                Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(fname));
            }
            fname += "/LogFile.txt";

            string path = System.Web.HttpContext.Current.Server.MapPath(fname);
            FileInfo finfo = new FileInfo(path);

            if (!finfo.Exists)
            {
                FileStream fs;
                fs = File.Create(path);
                fs.Close();
                finfo = new FileInfo(path);
            }

            /**/
            ///判断文件是否存在以及是否大于2K
            if (finfo.Length > 1024 * 1024 * 10)
            {
                fname = "/log/" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + "/LogFile" + DateTime.Now.TimeOfDay + ".txt";
                /**/
                ///文件超过10MB则重命名
                File.Move(path, System.Web.HttpContext.Current.Server.MapPath(fname));
                /**/
                ///删除该文件
                //finfo.Delete();
            }
            //finfo.AppendText();
            /**/
            ///创建只写文件流

            using (FileStream fs = finfo.OpenWrite())
            {
                /**/
                ///根据上面创建的文件流创建写数据流
                StreamWriter w = new StreamWriter(fs);

                /**/
                ///设置写数据流的起始位置为文件流的末尾
                w.BaseStream.Seek(0, SeekOrigin.End);

                /**/
                ///写入“Log Entry : ”
                w.Write("\n\rLog Entry : ");

                /**/
                ///写入当前系统时间并换行
                w.Write("{0} {1} \n\r", DateTime.Now.ToLongTimeString(),
                    DateTime.Now.ToLongDateString());

                /**/
                ///写入日志内容并换行
                w.Write(input + "\n\r");

                /**/
                ///写入------------------------------------“并换行
                w.Write("------------------------------------\n\r");

                /**/
                ///清空缓冲区内容，并把缓冲区内容写入基础流
                w.Flush();

                /**/
                ///关闭写数据流
                w.Close();
            }

        }
    }
}
